package ide.ccf.ccproject.wizards;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.operation.*;

import ide.ccf.ccproject.translate.Message;
import ide.ccf.ccproject.wizards.pages.ResourceWizardPage;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;

import java.io.*;

import org.eclipse.ui.*;
import org.eclipse.ui.ide.IDE;


/**
 * 新建资源文件向导
 *
 */
public class ResourceWizard extends Wizard implements INewWizard 
{
	private ResourceWizardPage page;
	private ISelection selection;

	/**
	 * 构造器
	 */
	public ResourceWizard() 
	{
		super();
		setNeedsProgressMonitor(true);
		setWindowTitle(Message.ResourceWizard_0);
	}
	
	/**
	 * 添加页面到向导
	 */
	public void addPages() 
	{
		page = new ResourceWizardPage(selection);
		addPage(page);
	}

	/**
	 * 向导完成
	 */
	public boolean performFinish() 
	{
		final String containerName = page.getContainerName();
		final String fileName = page.getFileName();
		
		IRunnableWithProgress op = new IRunnableWithProgress() 
		{
			public void run(IProgressMonitor monitor) throws InvocationTargetException 
			{
				try 
				{
					doFinish(containerName, fileName, monitor);
				} 
				catch (CoreException e) 
				{
					throw new InvocationTargetException(e);
				} 
				finally 
				{
					monitor.done();
				}
			}
		};
		
		try 
		{
			getContainer().run(true, false, op);
		} 
		catch (InterruptedException e) 
		{
			return false;
		} 
		catch (InvocationTargetException e) 
		{
			Throwable realException = e.getTargetException();
			MessageDialog.openError(getShell(), Message.ResourceWizard_1, realException.getMessage());
			return false;
		}
		return true;
	}
	
	/**
	 * 向导完成的真正实现方法
	 * @param containerName
	 * @param fileName
	 * @param monitor
	 * @throws CoreException
	 */
	private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException 
	{
		monitor.beginTask(Message.ResourceWizard_2 + fileName, 2);
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IResource resource = root.findMember(new Path(containerName));
		
		if (!resource.exists() || !(resource instanceof IContainer)) 
		{
			throwCoreException(Message.ResourceWizard_3 + containerName + Message.ResourceWizard_4);
		}
		
		IContainer container = (IContainer)resource;
		final IFile file = container.getFile(new Path(fileName));
		
		try 
		{
			InputStream stream = openContentStream();
			if (file.exists()) 
			{
				file.setContents(stream, true, true, monitor);
			} 
			else 
			{
				file.create(stream, true, monitor);
			}
			stream.close();
		} 
		catch (IOException e) 
		{
		}
		
		monitor.worked(1);
		monitor.setTaskName(Message.ResourceWizard_5);
		
		getShell().getDisplay().asyncExec(new Runnable() 
		{
			public void run() 
			{
				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				try 
				{
					IDE.openEditor(page, file, true);
				} 
				catch (PartInitException e) 
				{
				}
			}
		});
		monitor.worked(1);
	}
	
	/**
	 * 使用模板文本初始化文件内容
	 * @return
	 */
	private InputStream openContentStream() 
	{
		String contents ="<!DOCTYPE RCC>\n" + "<RCC version=\"1.0\">\n" + "\t<qresource prefix=\"/new/prefix1\">\n" + "\t</qresource>\n" + "</RCC>\n";
		return new ByteArrayInputStream(contents.getBytes());
	}

	/**
	 * 跑出核心异常
	 * @param message
	 * @throws CoreException
	 */
	private void throwCoreException(String message) throws CoreException 
	{
		IStatus status = new Status(IStatus.ERROR, "ide.ccf.ccproject", IStatus.OK, message, null);
		throw new CoreException(status);
	}

	/**
	 * 初始化
	 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) 
	{
		this.selection = selection;
	}
}